package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.ProxyCarPaymentDto;

/**
 * 待办缴费DAOIMP
 * 
 * @author Administrator
 *
 */
public class ProxyCarPaymentDaoImp extends TmsDao implements ProxyCarPaymentDao {

	protected ProxyCarPaymentDaoImp() throws TmsDaoException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * 根据车辆ID查询所有代缴费车辆信息
	 */
	@Override
	public List<ProxyCarPaymentDto> findProxyCarPaymentInfoBySerial(
			String serial) throws TmsDaoException {
		// 开始掉数据
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<ProxyCarPaymentDto> list = new ArrayList<ProxyCarPaymentDto>();
		
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("ID, ");
		sql.append("SERIAL, ");
		sql.append("AMOUNT, ");
		sql.append("STATUS, ");
		sql.append("CHARGETYPE, ");
		sql.append("INSERTUSERID, ");
		sql.append("INSERTTIME, ");
		sql.append("UPDATETIME, ");
		sql.append("UPDATEUSERID ");
		sql.append("FROM ");
		sql.append(TmsTableNames.TMS_PROXY_CAR_PAYMENT);
		sql.append(" WHERE ");
		sql.append(" SERIAL = '"+serial+"' ");

		ProxyCarPaymentDto pay = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();

			while (rs.next()) {
				pay = new ProxyCarPaymentDto();
				pay.setId(rs.getInt("ID"));
				pay.setSerial(rs.getString("SERIAL"));
				pay.setAmount(rs.getInt("AMOUNT"));
				pay.setStatus(rs.getInt("STATUS"));
				pay.setChargeType(rs.getInt("CHARGETYPE"));
				pay.setInsertUserId(rs.getInt("INSERTUSERID"));
				pay.setInsertTime(rs.getDate("INSERTTIME"));
				pay.setUpdateTime(rs.getDate("UPDATETIME"));
				pay.setUpdateUserId(rs.getInt("UPDATEUSERID"));
				list.add(pay);
			}
			
			return list;

		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}

	/**
	 * 
	 * 更新待办信息表信息
	 */
	@Override
	public void updateCarProxyBySerial(String serial, int outPay, int status,
			int chargStstus,int userId) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE ");
		sql.append(TmsTableNames.TMS_PROXY_CAR_PAYMENT);
		sql.append(" SET ");
		if(outPay > 0)
		{
			sql.append("AMOUNT = "+outPay+", ");
		}
		sql.append("CHARGETYPE = 1, ");
		sql.append("UPDATETIME =SYSDATE(), ");
		sql.append("UPDATEUSERID = "+userId+" ");
		sql.append(" WHERE ");
		sql.append(" SERIAL='"+serial+"' ");
		sql.append(" AND STATUS = "+status+" ");
		sql.append(" AND CHARGETYPE="+chargStstus+" ");

		try {
			ps = conn.prepareStatement(sql.toString());
		
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		
		
	}

	
	
}
